﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;


namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class DelSupplierCommandHanders : IRequestHandler<DelSupplierCommands, APIResult<Supplier>>
    {
        private readonly IBaseRepository<Supplier> supplierRepository;
        private readonly IMapper mapper;

        public DelSupplierCommandHanders(IBaseRepository<Supplier> supplierRepository, IMapper mapper)
        {
            this.supplierRepository = supplierRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 逻辑删除供应商信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<Supplier>> Handle(DelSupplierCommands request, CancellationToken cancellationToken)
        {
            APIResult<Supplier> result = new APIResult<Supplier>();
            result.Code = APIEnum.删除成功;
            result.Msg = "删除成功";
            var supplier = mapper.Map<Supplier>(request);
            var sql = $"Update Supplier set IsDelete=true where SupplierId='{request.SupplierId}'";
            await supplierRepository.Update(supplier, sql);
            return result;
        }
    }
}
